// pages/index/index.js - 严格按照test示例设计规范
Page({
  data: {
    searchKeyword: '',
    filters: ['放松舒缓', '稳定强化', '活动恢复', '姿态调整', '动作控制', '预防训练'],
    activeFilter: 0,
    recommendCourses: [
      {
        id: 1,
        title: '全身燃脂·高效训练',
        image: 'https://design.gemcoder.com/staticResource/echoAiSystemImages/0623c7a580d8110b2319fe65b06d4dd2.png',
        duration: '25分钟',
        tags: ['全身', '燃脂', '中级'],
        rating: 4.8,
        students: 1200
      }
    ],
    newsList: [
      {
        id: 1,
        title: '健身饮食指南：如何搭配你的训练餐',
        image: 'https://design.gemcoder.com/staticResource/echoAiSystemImages/6be859900a408d8a9fbf6965c617b150.png',
        time: '2小时前'
      },
      {
        id: 2,
        title: '运动后恢复的5个关键步骤',
        image: 'https://design.gemcoder.com/staticResource/echoAiSystemImages/ffad5df6e3073249a7b1e7bf90a03c35.png',
        time: '昨天'
      }
    ]
  },

  onLoad() {
    console.log('首页加载完成');
    this.checkLoginStatus();
  },

  onShow() {
    // 页面显示时检查登录状态
    this.checkLoginStatus();
  },

  // 检查登录状态
  checkLoginStatus() {
    const userInfo = wx.getStorageSync('userInfo');
    const token = wx.getStorageSync('token');

    if (!userInfo || !token) {
      // 用户未登录，跳转到登录页
      wx.redirectTo({
        url: '/pages/login/login'
      });
    }
  },

  // 搜索输入
  onSearchInput(e) {
    this.setData({
      searchKeyword: e.detail.value
    });
  },

  // 切换筛选条件
  toggleFilter(e) {
    const index = e.currentTarget.dataset.index;
    this.setData({
      activeFilter: index
    });
  },

  // 开始训练
  startTraining() {
    wx.navigateTo({
      url: '/pages/course/course?id=1'
    });
  },

  // 跳转到热门课程
  goToHotCourses() {
    wx.navigateTo({
      url: '/pages/course/course?type=hot'
    });
  },

  // 跳转到我的计划
  goToMyPlan() {
    wx.navigateTo({
      url: '/pages/plan/plan'
    });
  },

  // 跳转到挑战活动
  goToChallenge() {
    wx.showToast({
      title: '功能开发中',
      icon: 'none'
    });
  },

  // 跳转到个人中心
  goToProfile() {
    wx.switchTab({
      url: '/pages/profile/profile'
    });
  },

  // 跳转到全部课程
  goToAllCourses() {
    wx.navigateTo({
      url: '/pages/course/course?type=all'
    });
  },

  // 跳转到短时训练
  goToShortTraining() {
    wx.navigateTo({
      url: '/pages/course/course?type=short'
    });
  },

  // 跳转到高强度训练
  goToHighIntensity() {
    wx.navigateTo({
      url: '/pages/course/course?type=intensity'
    });
  },

  // 跳转到睡前放松
  goToRelax() {
    wx.navigateTo({
      url: '/pages/course/course?type=relax'
    });
  },

  // 查看更多推荐
  goToMore() {
    wx.navigateTo({
      url: '/pages/course/course?type=recommend'
    });
  },

  // 查看更多资讯
  goToMoreNews() {
    wx.showToast({
      title: '功能开发中',
      icon: 'none'
    });
  },

  // 跳转到资讯详情
  goToNewsDetail(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/detail/detail?type=news&id=${id}`
    });
  }
});
